;; Eulisp Module ;; Author: pab ;; File: zmodn.em ;; Date: Thu Feb 11 15:06:30 1993 ;; ;; Project: ;; Description: ;; Numbers modulo n in feel (defmodule zmodn ( eulisp0 ) () (defclass () ((n initarg n reader zmodn-class-n)) metaclass ) (defclass () ((z accessor zmodn-z)) metaclass ) (defun make-zmodn-class (n) (make 'direct-superclasses (list ) 'name (make-symbol (format nil "Zmod-~a" n)) 'n n)) (defconstant *zmodn-table* (make 'comparator = 'hash-function generic-hash)) (defun find-zmodn-class (n) (or (table-ref *zmodn-table* n) (let ((cl (make-zmodn-class n))) ((setter table-ref) *zmodn-table* n cl) cl))) ;; i mod n (defun make-modular-number (z n) (make (find-zmodn-class n) 'z z)) (defmethod initialize ((proto ) lst) (let ((i (call-next-method))) ((setter zmodn-z) i (remainder (scan-args 'z lst required-argument) (zmodn-n i))) i)) (defgeneric zmodn-n (obj)) (defmethod zmodn-n ((z )) (zmodn-class-n (class-of z))) ;; printing (on prin only, as this magically handles write too) (defmethod generic-prin ((i ) stream) (format stream "~a" (zmodn-z i) (zmodn-n i))) (defmethod binary+ ((i ) (j )) (when (compatible-moduli i j) (make-modular-number (+ (zmodn-z i) (zmodn-z j)) (zmodn-n i)))) (defun compatible-moduli (n m) (if (= (zmodn-n n) (zmodn-n m)) t (error "Incompatible-moduli" ))) ;; end module )